#!/usr/bin/python
# -*- coding: utf-8 -*-

"""Project Euler Solution 057

Copyright (c) 2011 by Robert Vella - robert.r.h.vella@gmail.com

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
"""

import cProfile
from euler.list_functions import take
from euler.numbers.decimal_base import number_of_digits
from euler.numbers.fractions import QuickFraction

def get_answer():
    """Question:
    
    It is possible to show that the square root of two can be expressed as an 
    infinite continued fraction.
    
    2**0.5 = 1 + 1/(2 + 1/(2 + 1/(2 + ... ))) = 1.414213...
    
    By expanding this for the first four iterations, we get:
    
    1 + 1/2 = 3/2 = 1.5
    1 + 1/(2 + 1/2) = 7/5 = 1.4
    1 + 1/(2 + 1/(2 + 1/2)) = 17/12 = 1.41666...
    1 + 1/(2 + 1/(2 + 1/(2 + 1/2))) = 41/29 = 1.41379...
    
    The next three expansions are 99/70, 239/169, and 577/408, but the eighth 
    expansion, 1393/985, is the first example where the number of digits in the 
    numerator exceeds the number of digits in the denominator.
    
    In the first one-thousand expansions, how many fractions contain a numerator 
    with more digits than denominator?
    """
    
    def numerator_has_more_digits(fraction):
        """Returns true if the numerator of [fraction] has more digits than its
        denominator.
        """
        
        return number_of_digits(fraction.numerator) \
                > number_of_digits(fraction.denominator)
        
    def root_two_expansions():
        """Generates the fractions which are expansions of the square root of 
        two.
        """
         
        current_expansion = QuickFraction(3, 2)
        
        #Yield the first number in the expansion of the square root of two.
        yield current_expansion
        
        #From close examination, it can be seen that the expansions of the square 
        #root of two can be generated by the recursive function, 
        #f(n) = 1 + 1/(1 + f(n - 1)). Yield all the fractions generated by this 
        #function.
        while True:                    
            #Yield the next number generated by the recursive function.
            current_expansion = 1 + QuickFraction(1, 1 + current_expansion)
            yield current_expansion
        
    
    #Return result.
    return sum(
              1 for fraction in take(1000, root_two_expansions()) 
                if numerator_has_more_digits(fraction)
            )
    
if __name__ == "__main__":
    cProfile.run("print(get_answer())")
